from functools import wraps
from flask import session, current_app, g


def user_login_data(view_func):
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        # 从session中取出用户的user_id
        user_id = session.get('user_id')
        # 同user_id取出用户对象
        user = None
        if user_id:
            try:
                from modules.models import User
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 将user数据封装到g对象中，方便后续使用
        g.user = user
        return view_func(*args, **kwargs)
    return wrapper
